<?php
set_time_limit(0);

include_once (dirname(__FILE__).'/config/database.php');
include_once (dirname(__FILE__).'/class/Log.inc');
include_once (dirname(__FILE__).'/class/Mysql/Exception.inc');
include_once (dirname(__FILE__).'/class/Mysql.inc');

define('LOCK_FILE', dirname(__FILE__).'/lock/selectItem.lock');
define('LOG_LEVEL', 'ERROR');

$LogFile = dirname(__FILE__).'/log/selectItem'.date('Y.m.d').'.log';

$Log = new Zw_Log();
$Log->setCurrLogLevel(LOG_LEVEL);
$Log->setLogFile($LogFile);

$handle = fopen(LOCK_FILE, 'w+');
if(!flock($handle, LOCK_EX|LOCK_NB)){
	$Log->write('进程已经启动，请不要重复启动');
	exit();
}

$GmDb = new Zw_Mysql();
$GmDb->setLog($Log);
$GmDb->connect($gmDatabaseHost, $gmDatabaseUsername, $gmDatabasePassword, $gmDatabaseName);

$GameDb = new Zw_Mysql();
$GameDb->setLog($Log);

$functionArr=array(14,91,168,86,186,33,137,189,193,236,221,230,233,250,258,259,185,128,225,236,15,16);
$item =array();
$operator = array('49you','sogou','yy','kuaiwan','qidian',
	'swjoy','9377','iqiyi','ufojoy','37',
	'xy','game2','602','360','51',
	'188wan','kugou','7477','7k7k','2144'
);
try {

	$row = 10000;
	$i = 1;
	$on = true;
	while ($on){
		$sql = "
			SELECT
				g.operatorName,a.itemClass,a.itemType,a.itemId,c.itemChineseName,a.functionId,b.functionChineseName,a.changeValue
			FROM
				`t_data_game_item` a
			LEFT JOIN
				t_cfg_item_function b ON a.functionId = b.functionId
			LEFT JOIN
				t_cfg_item c ON a.itemId = c.itemId
			LEFT JOIN
				t_data_game_account e ON a.userId = e.userId
			LEFT JOIN
				t_data_game_area f ON e.gameAreaId = f.gameAreaId
			LEFT JOIN
				t_data_operator g ON f.operatorId = g.operatorId
			LIMIT {$row}
			";
		$dataItem = $GmDb->getArray($sql);
		if($dataItem){
			$Log->write("执行第{$i}次");
			$i += 1;
			foreach($dataItem as $v){
				if (!isset($item[$v['itemClass']][$v['itemType']][$v['itemId']][$v['itemChineseName']][$v['functionId']][$v['functionChineseName']][$v['operatorName']])) {
					$item[$v['itemClass']][$v['itemType']][$v['itemId']][$v['itemChineseName']][$v['functionId']][$v['functionChineseName']][$v['operatorName']] = $v['changeValue'];
				} else {
					$item[$v['itemClass']][$v['itemType']][$v['itemId']][$v['itemChineseName']][$v['functionId']][$v['functionChineseName']][$v['operatorName']] += $v['changeValue'];
				}
			}
			$sql = "
				DELETE FROM `t_data_game_item` limit {$row}
			";
			$GmDb->query($sql);
		}else{
			$on = false;
		}
	}


	foreach($item as $k1=>$v1) {
		foreach ($v1 as $k2=>$v2) {
			foreach ($v2 as $k3=>$v3) {
				foreach($v3 as $k4=>$v4) {
					foreach ($v4 as $k5=>$v5) {
						foreach ($v5 as $k6=>$v6) {
							file_put_contents($LogFile,"{$k1}\t{$k2}\t{$k3}\t{$k4}\t{$k5}\t{$k6}\t", FILE_APPEND);
							foreach($operator as $x=>$y){
								if(isset($v6[$y])){
									file_put_contents($LogFile,"$v6[$y]\t", FILE_APPEND);
								}else{
									file_put_contents($LogFile,"0\t", FILE_APPEND);
								}
							}
							file_put_contents($LogFile,"\n", FILE_APPEND);
						}
					}
				}
			}
		}
	}
	//$Log->write("总数-".json_encode($item), 'ERROR');

}

catch(Zw_Mysql_Exception $e) {
	$Log->write('数据库错误', 'ERROR');
}
catch(Exception $e) {
	$Log->write('系统错误', 'ERROR');
}
